#include <QtGui>
#include "addtestdialog.h"
#include "ui_addtestdialog.h"


AddTestDialog::AddTestDialog(/*ChipsInfo::Designer designer, */QWidget *parent) :
    QDialog(parent),
//    _designer(designer),
    ui(new Ui::AddTestDialog)
{
    ui->setupUi(this);

    installUi();

    connect(ui->testsList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
            this, SLOT(changeTest(QListWidgetItem*,QListWidgetItem*)));
    connect(this, SIGNAL(startButtonsEnableReconcile()),
            this, SLOT(buttonsEnableReconcile()));

    setRevisions();
}

AddTestDialog::~AddTestDialog()
{
    delete ui;
}

void AddTestDialog::setRevisions()
{
    ChipsInfo::ChipGenerations generation = static_cast<ChipsInfo::ChipGenerations>(0); // add from designer version

    QList<QPair<QString, int> > revisionsList = ChipsInfo::revisionsList(generation);

    QPair<QString, int> revisionInfo;
    int revListCount = revisionsList.count();
    for (int i = 0; i < revListCount; ++i)
    {
        revisionInfo = revisionsList.at(i);
        ui->revisionsComboBox->addItem(revisionInfo.first, revisionInfo.second);
    }
}

void AddTestDialog::createIcons() const
{
    QListWidgetItem *projectButton = new QListWidgetItem(ui->testsList);
    projectButton->setIcon(QIcon(":/images/test.png"));
    projectButton->setText(tr("    Project test   "));
    projectButton->setTextAlignment(Qt::AlignHCenter);
    projectButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    QListWidgetItem *propertyButton = new QListWidgetItem(ui->testsList);
    propertyButton->setIcon(QIcon(":/images/test.png"));
    propertyButton->setText(tr("   Property test  "));
    propertyButton->setTextAlignment(Qt::AlignHCenter);
    propertyButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    QListWidgetItem *matrixButton = new QListWidgetItem(ui->testsList);
    matrixButton->setIcon(QIcon(":/images/test.png"));
    matrixButton->setText(tr("     Matrix test    "));
    matrixButton->setTextAlignment(Qt::AlignHCenter);
    matrixButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

    QListWidgetItem *paramsButton = new QListWidgetItem(ui->testsList);
    paramsButton->setIcon(QIcon(":/images/test.png"));
    paramsButton->setText(tr("Parameters test"));
    paramsButton->setTextAlignment(Qt::AlignHCenter);
    paramsButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}

void AddTestDialog::installUi() const
{
    createIcons();
    ui->testsList->setCurrentRow(0);

    ui->addButton->setEnabled(false);

    ui->upButton->setEnabled(false);
    ui->downButton->setEnabled(false);
    ui->removeButton->setEnabled(false);

    ui->allRevFrame->hide();

    ui->revisionFrame->hide();
}

void AddTestDialog::on_plusButton_clicked()
{
    QString text;
    text = ui->revisionsComboBox->currentText();
    ui->listWidget->addItem(text);

    emit startButtonsEnableReconcile();
}

void AddTestDialog::on_removeButton_clicked()
{
    if (!ui->listWidget->currentItem())
        return;

    ui->listWidget->takeItem(ui->listWidget->row(ui->listWidget->currentItem()));

    emit startButtonsEnableReconcile();
}

void AddTestDialog::changeTest(QListWidgetItem */*current*/, QListWidgetItem */*previous*/) const
{
    switch (ui->testsList->currentRow())
    {
    case TT_ProjectTest: {
        ui->projectFrame->show();
        ui->allRevFrame->hide();
        ui->revisionFrame->hide();
    }
        break;

    case TT_PropertyTest: {
        ui->projectFrame->hide();
        ui->allRevFrame->show();
        ui->revisionFrame->show();
    }
        break;

    case TT_MatrixTest: {
        ui->projectFrame->hide();
        ui->allRevFrame->show();
        ui->revisionFrame->show();
    }
        break;

    case TT_ParametersTest: {
        ui->projectFrame->hide();
        ui->allRevFrame->show();
        ui->revisionFrame->show();
    }
        break;

    default:
        return;
    }
}

void AddTestDialog::buttonsEnableReconcile() const
{
    ui->addButton->setEnabled(ui->listWidget->count() != 0);
    ui->upButton->setEnabled(ui->listWidget->count() != 0);
    ui->downButton->setEnabled(ui->listWidget->count() != 0);
    ui->removeButton->setEnabled(ui->listWidget->count() != 0);
}

